<template>
  <div id="my-tags">
    <el-tag
        :key="tag"
        v-for="tag in dynamicTags"
        closable
        type="danger"
        :disable-transitions="false"
        @close="handleClose(tag)">
      {{tag}}
    </el-tag>
    <el-input
        class="input-new-tag"
        v-if="inputVisible"
        v-model="inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
    >
    </el-input>
    <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
  </div>
</template>

<script>
import {putResume} from "@/api/resume";

export default {
  props:['skillTags','resumeId'],
  data() {
    return {
      dynamicTags:[],
      inputVisible: false,
      inputValue: ''
    };
  },
  created() {
    this.dynamicTags=this.skillTags.split(',')
  },
  methods: {
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      let str=this.dynamicTags.join(",")
      putResume({id:this.resumeId,skillLabel:str}).then(res=>{
        this.$message.success("更新成功")
      })
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
        let str=this.skillTags+','+inputValue
        putResume({id:this.resumeId,skillLabel:str}).then(res=>{
          this.$message.success("更新成功")
        })

      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
}
</script>

<style scoped>
.el-tag {
  margin: 5px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
#my-tags{
  margin-top: 10px;
  width: 85%;
  height: 100px;
  display: flex;
  flex-wrap: wrap;
}
</style>
